package com.cj.mobile.iabp.mvp.presenter;

import com.cj.mobile.iabp.BuildConfig;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.utils.RxUtils;
import com.cj.mobile.iabp.mvp.contract.HomeNewContract;
import com.cj.mobile.iabp.mvp.model.entity.DepotHomeResponse;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2023/11/15 17:19
 * ================================================
 */
@ActivityScope
public class HomeNewPresenter extends BasePresenter<HomeNewContract.Model, HomeNewContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    AccountManager mAccountManager;

    @Inject
    public HomeNewPresenter(HomeNewContract.Model model, HomeNewContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 界面初始化
     */
    public void initData() {
        getHomeInfo();
    }

    /**
     * 获取首页数据
     */
    public void getHomeInfo() {
        if (BuildConfig.IS_DEBUG_DATA) {
            mRootView.loadHomeInfo(new DepotHomeResponse(17,10,7,12));
        } else {
            mModel.getHomeInfo(mAccountManager.getUserid())
                    .compose(RxUtils.applySchedulers(mRootView))                                    // 使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<DepotHomeResponse>(mErrorHandler) {

                        @Override
                        public void onNext(DepotHomeResponse infoResponse) {
                            mRootView.loadHomeInfo(infoResponse);
                        }
                    });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAccountManager = null;

    }
}